<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/27 0027
 * Time: 17:02
 */
namespace app\home\controller;
use think\Db;


class Index  extends Base

{

    public function index(){
        //首页顶部广告1920x682
        $data_banner_1 = Db::name('banner')->where('class_id=30 and is_del =0 and is_show=1 and sign="pc"')->order('sort desc,id desc')->field(['out_link,banner_img'])->select();
        $this->assign('data_banner_1',$data_banner_1);
        
         $data_index = Db::name('zp')->where('is_recom = 1 and is_del =0 and is_show=1 and sign="pc"')->order('sort desc,id desc')->limit(4)->select();
        $this->assign('data_index',$data_index);
        
        
        
        
        
        
        
      /*
        
//首页每月客照欣赏广告1920x624
        $data_banner_2 = Db::name('banner')->where('type=2 and is_del =0 and is_show=1 and sign="pc"')->order('sort desc,id desc')->field(['out_link,banner_img'])->select();
        $this->assign('data_banner_2',$data_banner_2);
        //首页塞纳拍摄风格广告1920x624
//        $data_banner_3 = Db::name('banner')->where('type=3 and is_del =0 and is_show=1 and sign="pc"')->order('sort desc,id desc')->field(['out_link,banner_img'])->select();
//        $this->assign('data_banner_3',$data_banner_3);

        //首页基地下面视频
        $data_banner_3 = Db::name('video')->where('class_id=41 and is_del =0 and is_show=1 and is_recom=1 and sign="pc"')->find();
        $this->assign('data_banner_3',$data_banner_3);

        //首页热门拍摄/推荐
        $data_wn = Db::name('worknews')->where('is_del=0 and is_show=1 and is_recom=1 and sign="pc"')->order('id desc')->field(['title,id,seo_description,main_img'])->limit(4)->select();

        $this->assign('data_wn',$data_wn);

        //首页最新样片
        $data_zmyp= Db::name('zp')->alias('z')->join('jd j','z.cg_id=j.id')->join('style v','v.id=z.style_id')->where('z.is_del=0 and z.is_show=1 and z.is_recom=1 and z.sign="pc" ')->order('z.sort desc,z.id desc')->field(['z.main_img,z.seo_keywords,z.id,z.click,j.name,z.title,v.name as vname'])->limit(8)->select();

        $this->assign('data_zmyp',$data_zmyp);

        //首页基地下面广告benner
        $data_banneryp = Db::name('banner')->where('class_id=33 and is_del =0 and is_show=1 and sign="pc"')->order('sort desc,id desc')->field(['out_link,banner_img,seo_description'])->select();
        $this->assign('data_bannerpy',$data_banneryp);

        //场馆基地(顶部导航推荐)
        $data_cg = Db::name('psjd')->where('is_del=0 and is_show=1 and sign="pc" and is_recom=1')->order('sort desc,id desc')->limit(6)->select();
        $this->assign('data_cg',$data_cg);

        //推荐客片
        $data_wk = Db::name('works')->where('is_del=0 and is_show=1 and sign="pc" and is_recom=1')->order('sort desc,id desc')->limit(8)->select();
        $this->assign('data_wk',$data_wk);

        //首页底部视频
        $data_banner_font = Db::name('video')->where('class_id=42 and is_del =0 and is_show=1 and is_recom=1 and sign="pc"')->find();
        $this->assign('data_banner_font',$data_banner_font);

        //拍摄时间
        $where1 = 'is_del=0 and is_show=1 and sign="pc" and is_recom=1';
        $sql_1 = 'SELECT FROM_UNIXTIME(ps_time,\'%Y\') nian  FROM `dh_works` where '.$where1.'  GROUP BY nian ORDER BY nian DESC  LIMIT 1';
        $max_year =Db::name('works')->query($sql_1);
        $max_year =$max_year[0]['nian'];//最大的年份
        $this->assign('max_year',$max_year);
        $this->assign('month',getMonth(''));
        $sql_2 = 'SELECT FROM_UNIXTIME(ps_time,\'%Y\') nian  FROM `dh_works` where '.$where1.' GROUP BY nian ORDER BY nian DESC';
        $data_year = DB::name('works')->query($sql_2);
        $this->assign('data_year',$data_year);
        $sql_3 = 'SELECT FROM_UNIXTIME(ps_time,\'%m\') yue  FROM `dh_works` where '.$where1.' and FROM_UNIXTIME(ps_time,\'%Y\') ='.$max_year.'  GROUP BY yue ORDER BY yue DESC  LIMIT 1';
        $max_yue =Db::name('works')->query($sql_3);
        $max_yue=$max_yue[0]['yue'];//最大的月份
        $this->assign('max_yue',intval($max_yue));
        $time =$max_year.'-'.$max_yue;
        $max_time=strtotime($time);
        $where = "is_del=0 and is_show =1 and sign='pc' and is_recom=1 and ((FROM_UNIXTIME(ps_time,'%Y-%m') ='$max_year-$max_yue' or ps_time <=$max_time) and FROM_UNIXTIME(ps_time,'%Y')= $max_year   )";


        //每月客照欣赏
        $data_kz = Db::name('works')->where($where)->order('ps_time desc')->field("id,main_img,title, FROM_UNIXTIME(ps_time,'%Y.%m.%d') ps_time")->limit(6)->select();
        $this->assign('data_kz',$data_kz);
       //作品
        $data_zp = Db::name('zp')->where('is_recom = 1 and is_del=0 and is_show =1 and sign="pc"')->order('sort desc,id desc')->field("id,main_img,title,ps_time")->limit(8)->select();
        $this->assign('data_zp',$data_zp);
        //优势服务
        $data_aos = Db::name('aos')->where('is_recom = 1 and is_del=0 and is_show =1 and sign="pc"')
            ->order('sort desc,id desc')->limit(4)->select();
        $this->assign('data_aos',$data_aos);
        //视频
        $data_video = Db::name('vcr')->where('is_del=0 and is_show=1 and sign="pc"')->order('sort desc,id desc')->limit(2)->select();
        $this->assign('data_video',$data_video);
        //新品发布
        $data_news = Db::name('news')->where('is_recom=1 and is_del=0 and sign="pc"')->order('sort desc,id desc')->field('id,main_img,d_zan,fb_time,click,title,intro')->limit(3)->select();
        $this->assign("data_news",$data_news);
        */
//seo
        $this->assign('data_seo',getColumnseo(1));
    
        
 /**
 * Created by PhpStorm.
 * User: yuanbin
 * Date: 2018/10/13 
 *mote:头部公共数据
 * Time: 11.18
 */
 $this->publicIndex();
        
        //明星同款内景
        
        $inStar_arr['is_recom']=1;
        $inStar_arr['sign']="pc";
        $inStar_arr['type']="2";
        $inStar_arr['style_id']=27;
        $inStar = Db::name('zp')->where($inStar_arr)->limit(3)->order('id desc')->select();
        $this->assign('inStar',$inStar);
        
        
          //明星同款
        $sameStar_arr['is_recom']=1;
        $sameStar_arr['sign']="pc";
        $sameStar_arr['type']="2";
        $sameStar_arr['style_id']=31;
        $sameStar_arr = Db::name('zp')->where($sameStar_arr)->limit(5)->order('id desc')->select();
        $this->assign('sameStar_arr',$sameStar_arr);

        
                  //明星同款外景
        $outStar_arr['is_recom']=1;
        $outStar_arr['sign']="pc";
        $outStar_arr['type']="2";
        $outStar_arr['style_id']=28;
        $outStar = Db::name('zp')->where($outStar_arr)->limit(2)->order('id desc')->select();
        $this->assign('outStar',$outStar);

        
              //风格样片
        $kind_arr['is_recom']=1;
        $kind_arr['sign']="pc";
        $kind_arr['type']="1";
        $kind = Db::name('zp')->where($kind_arr)->limit(4)->order('id desc')->select();
        foreach($kind as $k =>$v)
        {
            $kind[$k]["create_time"]= date("Y-m-d",$v["create_time"]);
        }

        $this->assign('kind',$kind);
         //每周客照欣赏
       $data_kz = Db::name('works')->order('ps_time desc')->field("id,main_img,title, FROM_UNIXTIME(ps_time,'%Y.%m.%d') ps_time")->limit(6)->select();
       $this->assign('data_kz',$data_kz);
       
       
       
       //安娜特色
        $ppsj_arr['type']="4";
        $ppsj_arr['style_id']="39";
       $data_ana = Db::name('zp')->where($ppsj_arr)->order('ps_time desc')->limit(4)->select();
       $this->assign('data_ana',$data_ana);
       
       
  //视频
            $where_video["class_id"] = 41;
            $where_video["sign"] = "pc";
            $data_url =  Db::name('video')->where($where_video)->order('sort desc,id desc')->limit(1)->find();
                    
             $this->assign('data_url',$data_url);
        return $this->fetch();
    }
    
    
    
    
    public function details($id=0,$nid=0,$pid=0){

        if(!empty($id)){
            $where['id']=$id;
            $where = [
                'a.sign'=>"pc",
                'a.is_del'=>0,
                'a.is_show'=>1,
                'a.id'=>$id
            ];
            $data_kp = Db::name('aos')->alias('a')
                ->where($where)
                ->order('a.sort desc,a.id desc')
                ->find();
            $this->assign('data_kp',$data_kp);
        }else{
            return json(['code'=>0,'msg'=>'非法请求']);
        }
        //底部详情也广告
        $data_bannerfont = Db::name('banner')->where('is_del=0 and is_show=1 and sign="pc" and class_id=41')
            ->order('sort desc,id desc')->field('banner_img,out_link')->find();
        $this->assign('data_bannerfont',$data_bannerfont);

        //推荐客片
        $where = [
            'a.sign'=>"pc",
            'a.is_show'=>1,
            'a.is_del'=>0
        ];
        $data_kplist = Db::name('psjd')->alias('a')
            ->where($where)
            ->order('a.sort asc')
            ->paginate(12);
        $this->assign('data_kplist',$data_kplist);
        $this->assign('data_seo',getColumnseo(9));
        return $this->fetch();
    }



    public function changedate(){
        //改变年份，改变相应的月份
        $year = input('post.year');
        $where1='is_del=0 and is_show=1 and sign="pc"';
        $sql_1 ='SELECT CONCAT(FROM_UNIXTIME(ps_time,\'%Y\'),\'-\',FROM_UNIXTIME(ps_time,\'%m\')) times FROM `dh_works` where '.$where1.'  GROUP BY times';
        $year_month = Db::name('works')->query($sql_1);
        foreach ($year_month  as $k=>$v){
            $nian[]= explode('-',$v['times']);
        }
        foreach ($nian as $k=>$v){
            if(intval($v[0]) == intval($year)){
                $yue1[]=$v[1];
            }
        }
        $month= array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
        foreach ($month as $key =>$value){
            $month_2[$key][]=$value;
        }
        foreach ($month_2 as $k=>$v){
            foreach ($yue1 as $value){
                if(intval($k+1)  == intval($value)){
                    $month_2[$k]['class']='wxz cur';
                }
            }
        }
        //改变年份，改变相应的数据
            //通过年份获取该年份对应的最大的月份
            $sql ='SELECT FROM_UNIXTIME(ps_time,\'%m\') yue FROM `dh_works` WHERE FROM_UNIXTIME(ps_time,\'%Y\') ='.$year.' GROUP BY yue ORDER BY yue DESC  LIMIT 1' ;
            $max_yue =  Db::name('works')->query($sql);
            $max_yue=$max_yue[0]['yue'];
            //根据该年份和对应的最大的月份拼接处时间戳
            $time =$year.'-'.$max_yue;
            $max_time=strtotime($time);
            //找出对应
            $where = "is_del=0 and is_show =1 and sign='pc' and ((FROM_UNIXTIME(ps_time,'%Y-%m') ='$year-$max_yue') or ps_time <=$max_time)";
           //每月客照欣赏
            $data_kz = Db::name('works')->where($where)->order('ps_time desc,id desc')->field("id,main_img,title, FROM_UNIXTIME(ps_time,'%Y.%m.%d') ps_time")->limit(6)->select();
        return json(['code'=>1,'data'=>$month_2,'data2'=>$data_kz]);

    }

    public function guest(){
        //广告
        $data_banner = Db::name('banner')->where('is_del=0 and is_show=1 and sign="pc" and type=4')->order('sort desc,id desc')->field('banner_img,out_link')->select();
        $this->assign('data_banner',$data_banner);
        //
        $year= intval(input('param.y'));
        $month= intval(input('param.m'));
        $where ='is_del=0 and is_show=1 and sign="pc"';
        if($year){
            $where .= " and (FROM_UNIXTIME(ps_time,'%Y-%c') ='$year-$month')";
            $this->assign('year',$year);
            $this->assign('months',$month);
        }
        $data_works = Db::name('works')->where($where)->order('sort desc,id desc')->field('id,main_img,title,ps_time')->paginate(6);
        $this->assign('data_works',$data_works);
        //场馆基地(顶部导航推荐)
        $data_cg = Db::name('psjd')->where('is_del=0 and is_show=1 and sign="pc" and is_recom=1')->order('sort desc,id desc')->select();
        $this->assign('data_cg',$data_cg);

        $this->assign('month',getMonth(''));
        $sql_2 = 'SELECT FROM_UNIXTIME(ps_time,\'%Y\') nian  FROM `dh_works` GROUP BY nian ORDER BY nian DESC';
        $data_year = DB::name('works')->query($sql_2);
        $this->assign('data_year',$data_year);
        $sql_1 = 'SELECT FROM_UNIXTIME(ps_time,\'%Y\') nian  FROM `dh_works` GROUP BY nian ORDER BY nian DESC  LIMIT 1';
        $max_year =Db::name('works')->query($sql_1);
        $max_year =$max_year[0]['nian'];
        $this->assign('max_year',$max_year);
        return $this->fetch();
    }

    public function consult(){
        $data=[
            'mobile'=>input('post.mobile'),
            'sign'=>'pc',
            'create_time'=>time(),
        ];

        $result = Db::name('consult')->insert($data);
        if($result){
            //发送邮件提醒
            $content = '客户号码:'.$data['mobile'].',请您及时联系。[来自PC端]';
            send_mail('您的网站有新的客户信息',$content);
            return json(['code'=>1,'msg'=>'操作成功']);

        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    
    public function pslc(){
         $this->publicIndex();
       return $this->fetch();
    }
    
    
     public function about(){
         $this->publicIndex();
       return $this->fetch();
    }
    
    
}